Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
javascript. How to pass the "this" object after ajax call?

I have this code:

$(function(){
    $("#CASA").click(function(){
        rsExecute("rs/rs_luci.php","premi",1,function(cc){});
        var col=$( this ).css( "background-color" );
        setColor(this, 'yellow');
        setTimeout(setColor,1000,this,col);
    });
});

function setColor(obj,clr){
    $(obj).css( "background-color", clr );
}

rsExecute makes a call via ajax. function(cc){} is executed on success.

I've tried to change the code like this:

$(function(){
    $("#CASA").click(function(){
        rsExecute("rs/rs_luci.php","premi",1,function(cc){
            var col=$( this ).css( "background-color" );
            setColor(this, 'yellow');
            setTimeout(setColor,1000,this,col);
        });
    });
});

function setColor(obj,clr){
    $(obj).css( "background-color", clr );
}

but it doesn't work because this is undefined.

Is there a way to pass this object to the function inside rsExecute?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Save it to a variable, traditionally named 'that', and pass that variable using the closure.

$(function(){
    $("#CASA").click(function(){
        var that = this;
        rsExecute("rs/rs_luci.php","premi",1,function(cc){
            var col=$( that ).css( "background-color" );
            setColor(that, 'yellow');
            setTimeout(setColor,1000,that,col);
        });
    });
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!